"""
    这里测试代码
"""

from generator import test_n
from alg import *
import time
import numpy as np
import matplotlib
import matplotlib.pyplot as plt  # 画图的软件包
import csv
import sys

# 设置递归深度
sys.setrecursionlimit(10000)


# 生成数据集
test_list_disorder = []  # 无序数据集合
test_list_order = []  # 顺序数据集合
test_list_reserveOrder = []  # 逆序数据集合

# 从csv文件中读取数据
with open('./experiment_first/testListOrder.csv', 'r', encoding='utf-8', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        test_list_order.append([int(i) for i in row])

with open('./experiment_first/testListDisOrder.csv', 'r', encoding='utf-8', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        test_list_disorder.append([int(i) for i in row])

with open('./experiment_first/testListReserveOrder.csv', 'r', encoding='utf-8', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        test_list_reserveOrder.append([int(i) for i in row])

# 排序算法函数列表
sortFunction_list = [quick_sort, bubble_sort, merge_sort, heap_sort, insertion_sort, selection_sort]

# 记录输入规模为x时的运行时间
def runTime(sortFunction_list, test_list_i):
    run_time = []  # 构造一个算法运行时间的列表
    for sort_i in sortFunction_list:
        start = time.time()
        sort_i(test_list_i.copy())
        end = time.time()
        run_time.append(end - start)  # 记录运行时间

    return run_time  # 返回一个运行时间列表

run_time_orderlist = []  # 顺序数据运行时间列表
run_time_disorderlist = []  # 无序数据
run_time_reserveorderlist = []  # 逆序数据

# 遍历数据规模的列表，得到所有数据规模下的各个算法运行时间
for test_list_i in test_list_disorder:
    ls = runTime(sortFunction_list, test_list_i)

    run_time_disorderlist.append(ls)

for test_list_i in test_list_order:
    ls = runTime(sortFunction_list, test_list_i)

    run_time_orderlist.append(ls)

for test_list_i in test_list_reserveOrder:
    ls = runTime(sortFunction_list, test_list_i)

    run_time_reserveorderlist.append(ls)

# 画出输入规模与运行时间可视化的条形图
# 格式化默认字体
font = {'family': 'STKAITI',
        'weight': 'bold',
        'size': 15}
matplotlib.rc("font", **font)

# 输入规模与运行时间的条形图
def sortDataVisual(filename, runtime_list, test_n):
    bar_width = 0.15  # 每一个数据的宽度

    x_1 = list(range(len(runtime_list)))
    x_2 = [i+bar_width for i in x_1]
    x_3 = [i+2*bar_width for i in x_1]
    x_4 = [i+3*bar_width for i in x_1]
    x_5 = [i+4*bar_width for i in x_1]
    x_6 = [i+5*bar_width for i in x_1]

    # 设置绘图格式
    plt.figure(figsize=(15, 8), dpi=80)

    # 画图
    plt.bar(x_1, [ls[0] for ls in runtime_list], width=bar_width, label="快速排序")
    plt.bar(x_2, [ls[1] for ls in runtime_list], width=bar_width, label="冒泡排序")
    plt.bar(x_3, [ls[2] for ls in runtime_list], width=bar_width, label="归并排序")
    plt.bar(x_4, [ls[3] for ls in runtime_list], width=bar_width, label="堆排序")
    plt.bar(x_5, [ls[4] for ls in runtime_list], width=bar_width, label="插入排序")
    plt.bar(x_6, [ls[5] for ls in runtime_list], width=bar_width, label="选择排序")

    # # 设置x轴刻度
    plt.xticks(x_3, test_n)
    plt.xlabel('输入数据规模')
    plt.ylabel('排序算法运行时间')
    plt.ylim([0, 0.2])
    plt.title(filename)
    plt.legend()

    plt.show()
    pass

sortDataVisual("顺序数据的图像", run_time_orderlist, test_n)
sortDataVisual("逆序数据的图像", run_time_reserveorderlist, test_n)
sortDataVisual("无序数据的图像", run_time_disorderlist, test_n)

# 画出排序算法折线图
sortFunctionName = ["快速排序", "冒泡排序", "归并排序", "堆排序", "插入排序", "选择排序"]
fig = plt.figure(figsize=(20, 10), dpi=80)
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
for i in range(6):
    ax1.plot(test_n, [ls[i] for ls in run_time_orderlist], label=sortFunctionName[i])
    ax2.plot(test_n, [ls[i] for ls in run_time_reserveorderlist], label=sortFunctionName[i])
    ax3.plot(test_n, [ls[i] for ls in run_time_disorderlist], label=sortFunctionName[i])

ax1.set_title("顺序数据下的排序算法运行时间")
ax2.set_title("逆序数据下的排序算法运行时间")
ax3.set_title("无序数据下的排序算法运行时间")
ax1.set_xlabel("数据规模")
ax1.set_ylabel("运行时间")
ax2.set_xlabel("数据规模")
ax2.set_ylabel("运行时间")
ax3.set_xlabel("数据规模")
ax3.set_ylabel("运行时间")
ax1.legend()
ax2.legend()
ax3.legend()
plt.show()